//
//  YDLoginController.swift
//  YDReadFish
//
//  Created by rowena on 2019/3/31.
//  Copyright © 2019年 wuyezhiguhun. All rights reserved.
//

import UIKit
import YDExtension

class YDLoginController: UIViewController, YDLoginViewDelegate, YDLoginViewDataSource {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        addAllViews()
        setAllLayoutConstraint()

        // Do any additional setup after loading the view.
    }
    
    override func viewDidAppear(_ animated: Bool) {
        self.loginView.registerButton.isHidden = self.loginViewModel.registerButtonHidden()
    }
    
    lazy var loginView: YDLoginView = {
        let loginView = YDLoginView()
        loginView.delegate = self
        loginView.dataSource = self
        return loginView
    }()
    lazy var backgroundImageView: UIImageView = {
       let imageView = UIImageView(image: UIImage.yd_getLaunchPortrait())
        
        return imageView
    }()
    lazy var loginViewModel: YDLoginViewModel = {
        let viewModel = YDLoginViewModel()
        
        return viewModel
    }()

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

// MARK: ==== YDLoginViewDelegate ====
extension YDLoginController {
    /** 登陆 */
    func loginViewLogin(userName: String, userPassword: String) {
        if self.loginViewModel.loginUserInfo(userName: userName, userPassword: userPassword) {
            NotificationCenter.default.post(name: Notification.Name(YDNOTIFICATIONCENTERLOGINORREGISTER), object: nil)
            print("wuyezhiguhun -*- 登录成功")
        } else {
            print("wuyezhiguhun -*- 登录失败")
        }
        
        
//        if userName.count > 7 && userPassword.count > 7 {
//            NotificationCenter.default.post(name: Notification.Name(YDNOTIFICATIONCENTERLOGINORREGISTER), object: nil)
//        }
    }
    /** 注册 */
    func loginViewRegister(userName: String, userPassword: String) {
        if self.loginViewModel.registerUserInfo(userName: userName, userPassword: userPassword) {
            print("wuyezhiguhun -*- 注册成功")
        } else {
            print("wuyezhiguhun -*- 注册成功")
        }
        
        
//        if userName.count > 7 && userPassword.count > 7 {
//            NotificationCenter.default.post(name: Notification.Name(YDNOTIFICATIONCENTERLOGINORREGISTER), object: nil)
//        }
    }
    /** 清除数据 */
    func loginViewClearData() {
        self.loginViewModel.clearUserInfo()
//        NotificationCenter.default.post(name: Notification.Name(YDNOTIFICATIONCENTERLOGINORREGISTER), object: nil)
    }
    
    /** 获取注册按钮的状态 */
    func registerButtonHidden() -> Bool {
        return self.loginViewModel.registerButtonHidden()
    }
}

/** 添加View */
extension YDLoginController {
    
    /**
     * 添加所有的View
     */
    func addAllViews() {
        /** 添加背景图片 */
        self.view.addSubview(self.backgroundImageView)
        /** 添加登录LoginView */
        self.view.addSubview(self.loginView)
    }
    
}

/** 设置UI布局 */
extension YDLoginController {
    
    /**
     * 设置所有的页面布局
     */
    func setAllLayoutConstraint() {
        setBackgroundImageViewLayout()
        setLoginViewLayoutConstraint()
    }
    
    /** 设置背景ImageView */
    func setBackgroundImageViewLayout() {
        let topLayout = NSLayoutConstraint(item: self.backgroundImageView, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.view, attribute: NSLayoutConstraint.Attribute.top, multiplier: 1, constant: 0)
        let leftLayout = NSLayoutConstraint(item: self.backgroundImageView, attribute: NSLayoutConstraint.Attribute.left, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.view, attribute: NSLayoutConstraint.Attribute.left, multiplier: 1, constant: 0)
        let bottomLayout = NSLayoutConstraint(item: self.backgroundImageView, attribute: NSLayoutConstraint.Attribute.bottom, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.view, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1, constant: 0)
        let rightLayout = NSLayoutConstraint(item: self.backgroundImageView, attribute: NSLayoutConstraint.Attribute.right, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.view, attribute: NSLayoutConstraint.Attribute.right, multiplier: 1, constant: 0)
        
        self.view.addConstraint(topLayout)
        self.view.addConstraint(leftLayout)
        self.view.addConstraint(bottomLayout)
        self.view.addConstraint(rightLayout);
        
        self.backgroundImageView.translatesAutoresizingMaskIntoConstraints = false
    }
    
    /**
     * 设置登录页面布局
     */
    func setLoginViewLayoutConstraint() {
        let topLayout = NSLayoutConstraint(item: self.loginView, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.view, attribute: NSLayoutConstraint.Attribute.top, multiplier: 1, constant: 0)
        let leftLayout = NSLayoutConstraint(item: self.loginView, attribute: NSLayoutConstraint.Attribute.left, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.view, attribute: NSLayoutConstraint.Attribute.left, multiplier: 1, constant: 0)
        
        
//        let widthLayout = NSLayoutConstraint(item: self.loginView, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.width, multiplier: 1, constant: 400)
//        let heightLayout = NSLayoutConstraint(item: self.loginView, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.height, multiplier: 1, constant: 400)
        
        
        
        let bottomLayout = NSLayoutConstraint(item: self.loginView, attribute: NSLayoutConstraint.Attribute.bottom, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.view, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1, constant: 0)
        let rightLayout = NSLayoutConstraint(item: self.loginView, attribute: NSLayoutConstraint.Attribute.right, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.view, attribute: NSLayoutConstraint.Attribute.right, multiplier: 1, constant: 0)
        
        self.view.addConstraint(topLayout)
        self.view.addConstraint(leftLayout)
        self.view.addConstraint(bottomLayout)
        self.view.addConstraint(rightLayout);
        
        self.loginView.translatesAutoresizingMaskIntoConstraints = false
        
    }
    
    
}
